<?php

require_once("View/View/LoggedInView.php");
require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/MyWorkoutDAL.php");
require_once("Exercises/View/ExercisesView.php");
require_once("MyWorkouts/Model/WorkoutModel.php");
require_once("Database/Model/WorkoutExercisesDAL.php");


class MyWorkoutsView
{
	/**
	*@var ExerciseDAL 
	**/
	private $exerciseDAL;
	/**
	*@var LoggedInView 
	**/
	private $loggedInView;
	/**
	*@var MyWorkoutDAL 
	**/
	private $myWorkoutDAL;
	/**
	*@var ExerciseView 
	**/
	private $exerciseView;
	/**
	*@var MyWorkoutModel 
	**/
	private $workoutModel;
	/**
	*@var WorkoutExercisesDAL 
	**/
	private $workoutExercisesDAL;

	/**
	*@var string post 
	**/
	private static $newWorkoutName = "MyWorkoutsView::newWorkoutName";
	/**
	*@var string post 
	**/
	private static $newWorkoutDescription = "MyWorkoutsView::newWorkoutDescription";

	/**
	*@var string post 
	**/
	private static $editWorkoutName = "MyWorkoutsView::editWorkoutName";
	/**
	*@var string post 
	**/
	private static $editWorkoutDescription = "MyWorkoutsView::editWorkoutDescription";
	
	/**
	*@var string session
	**/
	private static $allExercises = "MyWorkoutsView::GetAllExercises";

	/**
	*@var string get 
	**/
	private static $add = "add";
	/**
	*@var string get 
	**/
	private static $added = "added";

	/**
	*@var string get 
	**/
	private static $edit = "edit";
	/**
	*@var string get 
	**/
	private static $edited = "edited";

	/**
	*@var string get 
	**/
	private static $idToRead = "read";
	/**
	*@var string get 
	**/
	private static $remove = "remove";


	public function __construct()
	{
		$this->exerciseDAL = new ExerciseDAL();
		$this->loggedInView = new LoggedInView();
		$this->myWorkoutDAL = new MyWorkoutDAL();
		$this->exerciseView = new ExersicesView();
		$this->workoutModel = new WorkoutModel();
		$this->workoutExercisesDAL = new workoutExercisesDAL();
	}

	/**
	*@return string html 
	**/
	public function getMyWorkoutsPage()
	{
		$html = $this->getMyWorkoutsPageHeader();
		$html .= $this->getMyWorkoutsPageBody();
		$html .= $this->getMyWorkoutsPageFooter();
		return $html;
	}

	/**
	*@return string html 
	**/
	private function getMyWorkoutsPageHeader()
	{
		$header = $this->loggedInView->getLoggedInHeader();
		return $header;
	}

	/**
	*@return string html 
	**/
	private function getMyWorkoutsPageBody()
	{
		$html = $this->getMenu();                   

	  	if($this->isItAdd())
	  	{
	  		$html .= $this->createNewWorkoutForm();
	  	}
	  	elseif($this->isItAdded())
	  	{
	  		$html .= $this->createNewWorkoutForm();
	  	}
	  	elseif($this->isItEdit())
	  	{
	  		$html .= $this->getEditForm();
	  	}
	  	elseif($this->ifReadWorkout())
	  	{
	  		$workoutID = $this->getIdToRead();
	  		$html .= $this->getAllWorkoutsHtml($workoutID);
	  		$html .= $this->getAllExercisesInOneWorkout($workoutID); 
	  	}
	  	else
	  	{
	  		$html .= "<div id='myWorkoutsContainer'>
				  	    <li>
							<a href='MyWorkouts.php?add'>Skapa nytt träningsupplägg<a/>
						</li>
				  </div>";
	  		$html .= $this->getAllWorkoutsHtml();
	  	}
	  
		return $html;

	}

	/**
	*@param int workoutID
	*@return string html 
	**/
	private function getAllWorkoutsHtml($workoutID = null)
	{
		$html = "<div id='WorkoutsDiv'>";
		$workouts;
		if($workoutID != null)
		{
			$workouts = $this->myWorkoutDAL->getWorkouts($workoutID); 
			if($workouts != null)
			{
				$html .= "<h2>" . $workouts->getName()        . "</h2>";
				$html .= "<p>"  . $workouts->getDescription() . "</h2>";
				$html .= "<a href='?remove=".$workouts->getID()."'> Ta bort</a>";
			}
			if($workouts == null)
			{
				$html .= "<p> Det här träningsschemat finns ej, eller är så har du ej behörighet att se det </p>";
			}
		}
		else
		{
			$workouts = $this->myWorkoutDAL->getWorkouts(); 
			if($workouts != null)
			{
				foreach($workouts as $wo)
				{
					$html .= "<h2>" . $wo->getName()        . "</h2>";
					$html .= "<p>"  . $wo->getDescription() . "</h2>";
					$html .= "<a href='?read=".$wo->getID()."'>Visa hela</a>";
					$html .= "<a href='?remove=".$wo->getID()."'> Ta bort</a>";
				}
				if($workouts == null)
				{
					$html .= "<p> Du har ej skapat några träningsscheman ännu</p>";
				}
			}
		}
	
		$html .= "</div>";
		return $html;
	}


	/**
	*@param int
	*@return array() Exercises 
	**/
	public function getAllExercisesInOneWorkout($workoutID)
 	{
 		$exercises = $this->workoutExercisesDAL->getAllWorkoutExercisesList($workoutID);
 		$exer = $this->exerciseView->getExercises("", $exercises);
 		return $exer;
 	}


 	/**
	*@return boolean 
	**/
 	private function ifReadWorkout()
 	{
 		if(isset($_GET[self::$idToRead]))
 		{
 			return true;
 		}
 		return false;
 	}

 	/**
	*@return int session 
	**/
 	private function getIdToRead()
 	{
 		if(isset($_GET[self::$idToRead]) && !empty($_GET[self::$idToRead]))
 		{
 			return $_GET[self::$idToRead];
 		}
 		return 0;
 	}


 	/**
	*@return string html
	**/
	private function getEditForm()
	{
		$html = "
		<div id='registerFormDiv' >
			<a href='MyWorkouts.php'>tillbaka</a>
			<form action='?added' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till övningar</legend>
					<label for='workoutName' >Namn :</label>
					<input type='text' size='40' name='" . self::$newWorkoutName . "' id='workoutName' value='' />
						
					<label for='exerciseDescription' >Beskrivning :</label>
					<textarea rows='4' cols='50' name='" . self::$newWorkoutDescription . "' id='exerciseDescription'></textarea>

					<input type='submit' name=''  value='Redigera' />
				</fieldset>
			</form>
		</div>";
	}

	/**
	*@return string html
	**/
	private function createNewWorkoutForm()
	{
		$message;
		$errormessage = $this->workoutModel->getErrorMessage();
		$successMessage = $this->workoutModel->getsuccesMessage();

		if($errormessage != "")
		{
			$message = "<div id='errorMessage'><p>$errormessage</p></div>";
		}
		elseif($successMessage != "")
		{
			$message = "<div id='successMessage'><p>$successMessage</p></div>";
		}

		$html = "
		<div id='registerFormDiv' >
			<a href='MyWorkouts.php'>tillbaka</a>
			<form action='?added' method='post' enctype='multipart/form-data'>
				<fieldset> 
					$message
					<legend>Lägg till övningar</legend>";

		$exercise = $this->exerciseDAL->getExerciseList();
		$eName ="";
		$eID = "";
		$html .= "<div id='ExercisesDiv'>";

		foreach($exercise as $ex)
		{
			$eName = $ex->getExerciseName();
			$eID = $ex->getExerciseID();
			//TODO, kanske spara till en array, där vi sätter namnet till id, och 
			// sparar alla id'n i en array till en session. sen loopar vi igenom den
			// och kollar om post är id text + id siffran.
			$html .= "<p>". $eName . " : <input type='checkbox' name='allAvailableWorkouts[]' value='".$eID."'/></p>";
		}

		$html .= "</div>";
		$html .= "  <label for='exerciseName' >Namn :</label>
					<input type='text' size='40' name='" . self::$newWorkoutName . "' id='exerciseName' value='' />
						
					<label for='exerciseDescription' >Beskrivning :</label>
					<textarea rows='4' cols='50' name='" . self::$newWorkoutDescription . "' id='exerciseDescription'></textarea>

					<input type='submit' name=''  value='Skapa' />
				</fieldset>
			</form>
		</div>";

		return $html;

	}

	/**
	*@return array
	**/
	public function getChosenExercises()
	{
		if(!empty($_POST['allAvailableWorkouts']))
		{	
			return $_POST['allAvailableWorkouts'];
		}
		return array();
	}

	/**
	*@return string
	**/
	public function getNewWorkoutDescription()
	{
		if(isset($_POST[self::$newWorkoutDescription]))
		{
			return $_POST[self::$newWorkoutDescription];
		}
	}

	/**
	*@return string
	**/
	public function getNewWorkoutName()
	{
		if(isset($_POST[self::$newWorkoutName]))
		{
			return $_POST[self::$newWorkoutName];
		}
	}
	


	/**
	*@return boolean
	**/
	public function isItRemove()
	{
		if(isset($_GET[self::$remove]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
	**/
	private function isItAdd()
	{
		if(isset($_GET[self::$add]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
	**/
	public function isItAdded()
	{
		if(isset($_GET[self::$added]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
	**/
	private function isItEdit()
	{
		if(isset($_GET[self::$edit]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
	**/
	public function isItEdited()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return int
	**/
	public function getIdToEdit()
	{
		if(isset($_GET[self::$edit]) && $_GET[self::$edit] != null)
		{
			return $_GET[self::$edit];
		}
		return 0;
	}

	/**
	*@return int
	**/
	public function getIdToRemove()
	{
		if(isset($_GET[self::$remove]) && $_GET[self::$remove] != null)
		{
			return $_GET[self::$remove];
		}
		return 0;
	}

	/**
	*@return int
	**/
	public function getSessionIdToEdit()
	{
		if(isset($_SESSION[self::$edit]) && $_SESSION[self::$edit] != null)
		{
			return $_SESSION[self::$edit];
		}
		return 0;
	}



	/**
	*@return string
	**/
	private function getMenu()
	{
		$html = $this->loggedInView->getLoggedInMenu();
	    return $html;
	}


	/**
	*@return string
	**/
	private function getMyWorkoutsPageFooter()
	{
		$footer = $this->loggedInView->getFooter();
		return $footer;
	}




}